{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.utils.data import Dataset, DataLoader\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "class My_DataSet(Dataset):\n",
    "    \"\"\"需要实现getitem和len魔法方法\"\"\"\n",
    "    def __init__(self):\n",
    "        # 获取数据\n",
    "        # readlines方法是将文件中的数据按行都读出来存入列表中\n",
    "        self.lines = open(r\"../../../../data/SMSSpamCollection\", mode=\"r\", encoding=\"UTF-8\").readlines()\n",
    "        \n",
    "    def __getitem__(self, index):  # 该方法可以使用对象[index]方法取值\n",
    "#         # strip是删除字符串开头和末尾指定的字符串，当不指定时是空格\n",
    "#         return self.lines[index].strip()\n",
    "        label = self.lines[index][:4].strip()\n",
    "        context = self.lines[index][4:].strip()\n",
    "        return label, context\n",
    "    \n",
    "    def __len__(self):\n",
    "        return len(self.lines)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5574\n",
      "557\n",
      "558\n",
      "[('ham', 'spam', 'ham', 'ham', 'spam', 'ham', 'ham', 'spam', 'ham', 'ham'), ('TELL HER I SAID EAT SHIT.', 'Sexy Singles are waiting for you! Text your AGE followed by your GENDER as wither M or F E.G.23F. For gay men text your AGE followed by a G. e.g.23G.', 'Actually nvm, got hella cash, we still on for  &lt;#&gt; ish?', \"When is school starting. Where will you stay. What's the weather like. And the food. Do you have a social support system like friends in the school. All these things are important.\", 'U have a secret admirer. REVEAL who thinks U R So special. Call 09065174042. To opt out Reply REVEAL STOP. 1.50 per msg recd. Cust care 07821230901', 'Here got ur favorite oyster... N got my favorite sashimi... Ok lar i dun say already... Wait ur stomach start rumbling...', 'Omg if its not one thing its another. My cat has worms :/ when does this bad day end?', 'Bored housewives! Chat n date now! 0871750.77.11! BT-national rate 10p/min only from landlines!', 'That way transport is less problematic than on sat night. By the way, if u want to ask  n  to join my bday, feel free. But need to know definite nos as booking on fri.', \"Lol your right. What diet? Everyday I cheat anyway. I'm meant to be a fatty :(\")]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "dataset = My_DataSet()\n",
    "# print(dataset[1])\n",
    "# 参数依次是：继承自DataSet类的对象；每个batch中的数据条数；是否打乱顺序，默认是False；\n",
    "# 如果最后一批不足batch_size个则删除，默认不会删除，将最后不够batch_size的条数组成一组；\n",
    "# num_worker是指定多少个线程一起加载数据\n",
    "data_loader = DataLoader(dataset=dataset, batch_size=10, shuffle=True, drop_last=True)\n",
    "print(len(dataset))\n",
    "print(len(data_loader))  # len(dataset)/batch_size，如果不指定drop_last=True，则默认是向上取整\n",
    "print(math.ceil(len(dataset)/10))  # 向上取整\n",
    "for i in data_loader:  # 如果使用多个线程会报错：BrokenPipeError: [Errno 32] Broken pipe\n",
    "    print(i)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
